package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Ad;
import com.abaaba.project2.bean.AdExample;
import com.abaaba.project2.bean.bo.AdReqBo;
import com.abaaba.project2.bean.bo.AddAdvertisementReqBo;
import com.abaaba.project2.bean.bo.BaseReqBo;
import com.abaaba.project2.bean.data.AdvertisementInfo;
import com.abaaba.project2.bean.vo.AdvertisementListVO;
import com.abaaba.project2.bean.vo.AdvertisementVo;
import com.abaaba.project2.mapper.AdMapper;
import com.abaaba.project2.service.AdvertisementService;
import com.abaaba.project2.util.DateToStringUtil;
import com.abaaba.project2.util.PageHelperUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AdvertisementServiceImpl implements AdvertisementService {

    @Autowired
    AdMapper adMapper;

    //注意：这里的AdReqBo对象继承自BaseReqBo，因为list接口有两种请求的方式（请求参数不同）
    @Override
    public AdvertisementListVO queryAd(AdReqBo baseReqBO) {

        String name = baseReqBO.getName();
        String content = baseReqBO.getContent();

        AdvertisementListVO advertisementListVO = null;
        if (name == null && content == null) {
            advertisementListVO = queryAllAd(baseReqBO);
        } else {
            advertisementListVO = queryAdByNameAndContent(baseReqBO);
        }
        return advertisementListVO;
    }


    @Override
    public AdvertisementVo createAdvertisement(AddAdvertisementReqBo addAdvertisementReqBo) {

        //条件：向数据库中直接插入即可

        Ad ad = new Ad();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String format = df.format(new Date()); //获取当前时间并转化为字符串

        ad.setContent(addAdvertisementReqBo.getContent());
        ad.setEnabled(addAdvertisementReqBo.getEnabled());
        ad.setName(addAdvertisementReqBo.getName());
        ad.setPosition(addAdvertisementReqBo.getPosition());
        ad.setUrl(addAdvertisementReqBo.getUrl());
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        ad.setAddTime(date);
        ad.setUpdateTime(date);
        //将记录插入到数据库中
        adMapper.insertSelective(ad);
        Integer id = ad.getId(); //获取id值

        //从数据库中查询出该条记录的相关信息(根据插入时间进行查询)
        //创建example对象
        AdExample adExample = new AdExample();
        AdExample.Criteria criteria = adExample.createCriteria();


        AdvertisementVo advertisementVo = new AdvertisementVo();
        advertisementVo.setAddTime(ad.getAddTime().toString());
        advertisementVo.setContent(ad.getContent());
        advertisementVo.setEnabled(ad.getEnabled());
        advertisementVo.setId(ad.getId());
        advertisementVo.setName(ad.getName());
        advertisementVo.setPosition(ad.getPosition());
        advertisementVo.setUpdateTime(ad.getUpdateTime().toString());
        advertisementVo.setUrl(ad.getUrl());


        return advertisementVo;
    }

    @Override
    public AdvertisementInfo updateAdvertisement(AdvertisementInfo advertisementInfo) {

        //更新的条件：无

        //获取example对象
        AdExample adExample = new AdExample();
        AdExample.Criteria criteria = adExample.createCriteria();

        //创建Ad对象
        Ad ad = new Ad();
        ad.setId(advertisementInfo.getId());
        ad.setName(advertisementInfo.getName());
        ad.setUrl(advertisementInfo.getUrl());
        ad.setContent(advertisementInfo.getContent());
        ad.setEnabled(advertisementInfo.getEnabled());
        ad.setLink(advertisementInfo.getLink());
        Date date = new Date(System.currentTimeMillis());
        ad.setUpdateTime(date);

        adMapper.updateByPrimaryKeySelective(ad);

        Ad ad1 = adMapper.selectByPrimaryKey(advertisementInfo.getId());
        AdvertisementInfo advertisementInfo1 = new AdvertisementInfo();
        advertisementInfo1.setId(ad1.getId());
        advertisementInfo1.setName(ad1.getName());
        advertisementInfo1.setLink(ad1.getLink());
        advertisementInfo1.setUrl(ad1.getUrl());
        advertisementInfo1.setPosition(ad1.getPosition());
        advertisementInfo1.setContent(ad1.getContent());
        advertisementInfo1.setEnabled(ad1.getEnabled());
        advertisementInfo1.setAddTime(ad1.getAddTime().toString());
        advertisementInfo1.setUpdateTime(ad1.getUpdateTime().toString());
        advertisementInfo1.setEnabled(ad1.getEnabled());

        return advertisementInfo1;
    }

    @Override
    public void deleteAdvertisement(int id) {
        //条件：根据id进行逻辑删除，将deleted的值改为1
        //创建example对象
        AdExample adExample = new AdExample();
        AdExample.Criteria criteria = adExample.createCriteria();

        //拼接sql语句
        criteria.andIdEqualTo(id);
        Ad ad = new Ad();
        ad.setId(id);
        ad.setDeleted(true);
        adMapper.updateByPrimaryKeySelective(ad);
    }


    private AdvertisementListVO queryAdByNameAndContent(AdReqBo baseReqBO) {
        //使用工具类PageHelper进行分页查询
        PageHelperUtil.startPage(baseReqBO);

        //查询条件：根据name和Content进行模糊查询
        String name = baseReqBO.getName();
        String content = baseReqBO.getContent();
        //创建example对象
        AdExample adExample = new AdExample();

        //排序信息
        adExample.setOrderByClause(baseReqBO.getSort()+" "+baseReqBO.getOrder());

        AdExample.Criteria criteria = adExample.createCriteria();
        //拼接sql语句
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (content != null && !"".equals(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        criteria.andDeletedEqualTo(false);

        //从数据库中查询到所有的记录
        List<Ad> ads = adMapper.selectByExample(adExample);

        //获得分页信息
        PageInfo<Ad> adPageInfo = new PageInfo<>(ads);
        long total = adPageInfo.getTotal();

        AdvertisementListVO advertisementListVO = new AdvertisementListVO();
        advertisementListVO.setTotal((int) total);

        List<AdvertisementInfo> advertisementInfos = new ArrayList<>();

        for (Ad ad : ads) {
            AdvertisementInfo advertisementInfo = new AdvertisementInfo();
            advertisementInfo.setId(ad.getId());
            advertisementInfo.setName(ad.getName());
            advertisementInfo.setLink(ad.getLink());
            advertisementInfo.setUrl(ad.getUrl());
            advertisementInfo.setPosition(ad.getPosition());
            advertisementInfo.setContent(ad.getContent());
            advertisementInfo.setEnabled(ad.getEnabled());

            Date date1 =  ad.getAddTime();
            String addTime = DateToStringUtil.dateToString(date1);
            advertisementInfo.setAddTime(addTime);

            Date date2 = ad.getUpdateTime();
            String updateTime = DateToStringUtil.dateToString(date2);
            advertisementInfo.setUpdateTime(updateTime);

//            advertisementInfo.setAddTime(ad.getAddTime().toString());
//            advertisementInfo.setUpdateTime(ad.getUpdateTime().toString());
            advertisementInfo.setDeleted(ad.getDeleted());
            advertisementInfos.add(advertisementInfo);
        }
        advertisementListVO.setItems(advertisementInfos);
        return advertisementListVO;
    }

    private AdvertisementListVO queryAllAd(AdReqBo baseReqBO) {
        //使用工具类PageHelper进行分页查询
        PageHelperUtil.startPage(baseReqBO);

        //查询条件：deleted = 0（即没有被逻辑删除掉的记录），这里直接从数据库查询出所有广告记录的相关信息即可

        //创建example对象
        AdExample adExample = new AdExample();

        //排序信息
        adExample.setOrderByClause(baseReqBO.getSort()+" "+baseReqBO.getOrder());

        AdExample.Criteria criteria = adExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        //从数据库中查询到所有的记录
        List<Ad> ads = adMapper.selectByExample(adExample);

        //获得分页信息
        PageInfo<Ad> adPageInfo = new PageInfo<>(ads);
        long total = adPageInfo.getTotal();

        AdvertisementListVO advertisementListVO = new AdvertisementListVO();
        advertisementListVO.setTotal((int) total);

        List<AdvertisementInfo> advertisementInfos = new ArrayList<>();

        for (Ad ad : ads) {
            AdvertisementInfo advertisementInfo = new AdvertisementInfo();
            advertisementInfo.setId(ad.getId());
            advertisementInfo.setName(ad.getName());
            advertisementInfo.setLink(ad.getLink());
            advertisementInfo.setUrl(ad.getUrl());
            advertisementInfo.setPosition(ad.getPosition());
            advertisementInfo.setContent(ad.getContent());
            advertisementInfo.setEnabled(ad.getEnabled());

            //利用工具类进行日期格式转换
            Date date1 =  ad.getAddTime();
            String addTime = DateToStringUtil.dateToString(date1);
            advertisementInfo.setAddTime(addTime);

            Date date2 = ad.getUpdateTime();
            String updateTime = DateToStringUtil.dateToString(date2);
            advertisementInfo.setUpdateTime(updateTime);
//            advertisementInfo.setAddTime(ad.getAddTime().toString());
//            advertisementInfo.setUpdateTime(ad.getUpdateTime().toString());
            advertisementInfo.setDeleted(ad.getDeleted());
            advertisementInfos.add(advertisementInfo);
        }
        advertisementListVO.setItems(advertisementInfos);
        return advertisementListVO;
    }
}
